package com.wencai.daily;

import com.wencai.daily.bean.StatisticResult;
import com.wencai.util.ExportUtils;
import com.wencai.util.IOUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wangpan
 * @date 2023/6/6
 */
public class CombineResult {
    private static String year = "2021";
    private static String dir = "D:\\Users\\Administrator\\Desktop\\日线策略\\统计数据汇总\\" + year + "\\";
    private static String outPath = "D:\\Users\\Administrator\\Desktop\\日线策略\\统计结果_" + year + ".xlsx";

    public static void main(String[] args) throws IOException {
        File dirFile = new File(dir);
        String[] children = dirFile.list();
        List<StatisticResult> results = new ArrayList<>();
        for (String file : children) {
            if (file.endsWith(".txt")) {
                List<String> list = IOUtils.readFromFile(dir + file);
                StatisticResult result = getStatisticResult(file, list);
                results.add(result);
            }
        }
        ExportUtils.export(getHead(), results, StatisticResult.class, outPath);
    }

    private static StatisticResult getStatisticResult(String file, List<String> list) {
        double rate = Double.valueOf(file.replace(".txt", ""));
        String s = list.get(0);
        String avg = s.replaceAll("average result is :", "").trim();
        double avgDouble = Double.valueOf(avg);
        int minIndex = findIndex(list, "minList:");
        if (minIndex < 0) {
            throw new RuntimeException("Index < 0");
        }
        String min = list.get(minIndex + 1);
        double minGainRate = findGainRate(min);

        int maxIndex = findIndex(list, "maxList:");
        if (maxIndex < 0) {
            throw new RuntimeException("Index < 0");
        }
        double max1GainRate = findGainRate(list.get(maxIndex + 1));
        double max2GainRate = findGainRate(list.get(maxIndex + 2));
        double max3GainRate = findGainRate(list.get(maxIndex + 3));
        double max4GainRate = findGainRate(list.get(maxIndex + 4));
        double max5GainRate = findGainRate(list.get(maxIndex + 5));

        double maxAvg = (max1GainRate + max2GainRate + max3GainRate + max4GainRate + max5GainRate) / 5;
        return new StatisticResult(rate, max1GainRate, minGainRate, avgDouble, maxAvg, findStart(list.get(maxIndex + 1)), findEnd(list.get(maxIndex + 1)));
    }

    private static double findGainRate(String max1) {
        String str = max1.replaceAll("CalculateResult\\{startTimePoint='\\d+', endTimePoint='\\d+', gainRate=([\\.\\d]+),.*", "$1");
        return Double.valueOf(str);
    }

    private static String findStart(String max1) {
        return max1.replaceAll("CalculateResult\\{startTimePoint='(\\d+)', endTimePoint='\\d+', gainRate=([.\\d]+),.*", "$1");
    }

    private static String findEnd(String max1) {
        return max1.replaceAll("CalculateResult\\{startTimePoint='(\\d+)', endTimePoint='(\\d+)', gainRate=([.\\d]+),.*", "$2");
    }

    private static int findIndex(List<String> list, String s) {
        for (int i = 0; i < list.size() - 1; i++) {
            if (list.get(i).trim().equals(s)) {
                return i;
            }
        }
        return -1;
    }

    private static List<List<String>> getHead() {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("量比"));
        head.add(Collections.singletonList("最大收益率"));
        head.add(Collections.singletonList("最小收益率"));
        head.add(Collections.singletonList("平均收益率"));
        head.add(Collections.singletonList("最大平均收益率"));
        head.add(Collections.singletonList("最大收益率买点"));
        head.add(Collections.singletonList("最大收益率卖点"));
        return head;
    }
}
